
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

public class CarClient extends JFrame
{
  private PrintWriter pw;  
//  private JFrame frame;  
  private JPanel pane_buttom;  
  private JSplitPane pane_center;  

  //显示内容的文本框，输入内容的文本框,发送内容按钮  
  private JScrollPane pane_showWindow;  
  private JScrollPane pane_inputWindow;  
  private JTextArea area_showWindow;  
  private JTextArea area_inputWindow;  

  private JButton btn_send;
  private JButton btn_left;
  private JButton btn_right;
  private JButton btn_ret;
  private JButton btn_faw;
  private JButton btn_stop;
  private JButton btn_fast;
  private JButton btn_slow;
  private Dimension dimension;//用于设置area_showWindow可拖拉的大小  


  //初始化  
  public CarClient() {  
//      frame = new JFrame();  
      pane_buttom = new JPanel();  
      pane_showWindow = new JScrollPane();  
      pane_inputWindow = new JScrollPane();  
      area_showWindow = new JTextArea();  
      area_inputWindow = new JTextArea();  
      pane_center = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, pane_showWindow, pane_inputWindow);  
      btn_send = new JButton("发送");  
      btn_faw = new JButton ("前进");
      btn_ret = new JButton ("后退");
      btn_left = new JButton ("左");
      btn_right = new JButton("右");
      btn_stop = new JButton("停止");
      btn_fast = new JButton("加速");
      btn_slow = new JButton("减速");
      dimension = new Dimension(50, 300);  
  }  

  //调用方法显示窗口  
  public void showFrame(){  
      initFrame();  
      initChatTextArea();  
      initButton();  
      btn_send();  
      socket();  
  }  

  //主窗体  
  public void initFrame(){  
      this.setTitle("客户端");  
      int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();  
      int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();  
      this.setBounds(width / 2, height / 2, 400, 450);  
      this.setVisible(true);  
  }  

  //内容显示文本框和输入内容文本框  
  private void initChatTextArea(){  
      //取得视图焦点  
      pane_showWindow.getViewport().add(area_showWindow);  
//      pane_inputWindow.getViewport().add(area_inputWindow);
      pane_inputWindow.add (btn_faw);
      pane_inputWindow.add (btn_ret);
      pane_inputWindow.add (btn_left);
      pane_inputWindow.add (btn_right);
      pane_inputWindow.add (btn_stop);
      pane_inputWindow.add (btn_fast);
      pane_inputWindow.add (btn_slow);
      //将显示文本域设置为不可编辑  
      area_showWindow.setEditable(false);  
      //设置显示文本域可拖拉的大小   
      pane_showWindow.setMinimumSize(dimension);  
      this.add(pane_center, BorderLayout.CENTER);  
  }  

  //发送文件，发送内容按钮  
  public void initButton(){  
      pane_buttom.add(btn_send);
      pane_buttom.add (btn_faw);
      pane_buttom.add (btn_ret);
      pane_buttom.add (btn_left);
      pane_buttom.add (btn_right);
      pane_buttom.add (btn_stop);
      pane_buttom.add (btn_fast);
      pane_buttom.add (btn_slow);
      this.add(pane_buttom, BorderLayout.SOUTH);  
  }  

  private void btn_send(){  
      btn_send.addActionListener(new ActionListener() {  

          @Override  
          public void actionPerformed(ActionEvent e) {  
              String info = area_inputWindow.getText();  
              area_showWindow.append("客户端： "+info+"\r\n");  
              pw.println(info);  
              area_inputWindow.setText("");  
          }  
      });
      btn_faw.addActionListener (new ActionListener() {  

        @Override  
        public void actionPerformed(ActionEvent e) {  
            area_showWindow.append("客户端： 前进\r\n");  
            pw.println("faw");  
        }  
      });
      
      btn_left.addActionListener (new ActionListener() {  

        @Override  
        public void actionPerformed(ActionEvent e) {  
            area_showWindow.append("客户端： 向左\r\n");  
            pw.println("left");  
        }  
      });
      btn_ret.addActionListener (new ActionListener() {  

        @Override  
        public void actionPerformed(ActionEvent e) {  
            area_showWindow.append("客户端：后退\r\n");  
            pw.println("ret");  
        }  
      });
      btn_right.addActionListener (new ActionListener() {  

        @Override  
        public void actionPerformed(ActionEvent e) {  
            area_showWindow.append("客户端： 向右\r\n");  
            pw.println("right");  
        }  
      });
      btn_stop.addActionListener (new ActionListener() {  

        @Override  
        public void actionPerformed(ActionEvent e) {  
            area_showWindow.append("客户端：停止\r\n");  
            pw.println("stop");  
        }  
      });
      btn_fast.addActionListener (new ActionListener() {  

          @Override  
          public void actionPerformed(ActionEvent e) {  
              area_showWindow.append("客户端：加速\r\n");  
              pw.println("fast");  
          }  
        });
      btn_slow.addActionListener (new ActionListener() {  

          @Override  
          public void actionPerformed(ActionEvent e) {  
              area_showWindow.append("客户端：减速\r\n");  
              pw.println("slow");  
          }  
        });
//      this.addKeyListener (new KeyAdapter()
//      {
//        
//        @Override
//        public void keyTyped (KeyEvent e)
//        {
//        	System.out.println("key typed");
//          if(e.getKeyCode()==KeyEvent.VK_UP){
//              btn_faw.doClick ();
//          }
//           else if(e.getKeyCode()==KeyEvent.VK_DOWN){
//             btn_ret.doClick ();
//           }else if(e.getKeyCode()==KeyEvent.VK_LEFT){
//             btn_left.doClick ();
//           }else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
//             btn_right.doClick ();
//           }else {
//             btn_stop.doClick ();
//           }
//          
//        }
//        
//        @Override
//        public void keyReleased (KeyEvent e)
//        {
//        	System.out.println("key released");
//        }
//        
//        @Override
//        public void keyPressed (KeyEvent e)
//        {
//
//        	System.out.println("key press");
//        }
//      });
      
      Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
    	  public void eventDispatched(AWTEvent event) {
//    		  if(((KeyEvent) event).getKeyCode()==KeyEvent.VK_UP){
//                  btn_faw.doClick ();
//              }
//               else if(((KeyEvent) event).getKeyCode()==KeyEvent.VK_DOWN){
//                 btn_ret.doClick ();
//               }else if(((KeyEvent) event).getKeyCode()==KeyEvent.VK_LEFT){
//                 btn_left.doClick ();
//               }else if(((KeyEvent) event).getKeyCode()==KeyEvent.VK_RIGHT){
//                 btn_right.doClick ();
//               }else {
//                 btn_stop.doClick ();
//               }
    	  	}
    	  }, AWTEvent.KEY_EVENT_MASK);
  }  
    
  private void socket(){  
      try {  
          Socket s = new Socket("192.168.1.106",10086);  
          InputStreamReader isr=new InputStreamReader(s.getInputStream());  
          BufferedReader br=new BufferedReader(isr);  
          pw=new PrintWriter(s.getOutputStream(),true);  
          while(true){  
              //不停地读取从服务器端发来的信息  
              String info=br.readLine();  
              area_showWindow.append("服务端： "+info+"\r\n");  
          }  
      } catch (UnknownHostException e) {  
          e.printStackTrace();  
      } catch (IOException e) {  
          e.printStackTrace();  
      }  
  }  
  public static void main(String[] args) {  
    CarClient chat = new CarClient();  
      chat.showFrame();  
  }

}
